package application;

import application.dao.DBService;
import application.dao.entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import java.net.URL;
import java.util.ResourceBundle;

public class Athlete_personal_information_controller extends BaseController implements Initializable {

    public TextField userName;
    public TextField realName;
    public PasswordField password;
    public PasswordField surePassword;
    public TextField idCard;
    public TextField mobile;
    public TextField address;
    @FXML
    private Button change_info;

    @FXML
    private Menu menu_enroll;

    @FXML
    private Menu menu_main;

    @FXML
    private MenuItem menu_informations_announcements;

    @FXML
    private MenuItem menu_informations_news;

    @FXML
    private Button enroll_yes;

    @FXML
    private Menu menu_person;

    @FXML
    private Menu menu_scores;

    @FXML
    void enroll_yes(ActionEvent event) {
        System.out.println("确认");
        if (!surePassword.getText().equals(password.getText())) {
            BaseUtil.info("密码不一致");
            return;
        }
        User user = createUser();
        boolean b = DBService.updateUser(user);
        BaseUtil.gotoMain(b, "更新失败", stage);
    }

    private User createUser() {
        User loginUser = Global.loginUser;
        loginUser.setPassWord(password.getText());
        loginUser.setTelNumber(mobile.getText());
        loginUser.setHomeAddress(address.getText());
        return Global.loginUser;
    }

    @FXML
    void onButton_change_info(ActionEvent event) {
        System.out.println("重置");
        clearData();
    }

    private void clearData() {
        BaseUtil.clearText(userName, realName, idCard, mobile, address, password, surePassword);
    }

    @FXML
    void onCheckMenu_main(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_person(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_enroll(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_scores(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations_announcements(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations_news(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        init();
    }


    @Override
    public javafx.stage.Stage setStage(javafx.stage.Stage stage) {
        return null;
    }

    @Override
    public void init() {
        User loginAdmin = Global.loginUser;
        userName.setText(loginAdmin.getUserName());
        realName.setText(loginAdmin.getRealName());
        idCard.setText(loginAdmin.getIdCard());
        mobile.setText(loginAdmin.getTelNumber());
        address.setText(loginAdmin.getHomeAddress());
        password.setText(loginAdmin.getPassWord());
        surePassword.setText(loginAdmin.getPassWord());
    }
}